package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class SysDeptServiceImpl implements ISysDeptService {
    @Autowired
    SysDeptMapper sysDeptMapper;
    @Autowired
    SysDictDataMapper sysDictDataMapper;
    @Autowired
    SysRoleMapper roleMapper;


    public List<SysDictData> selectAllSysDictdata(String dicts) {
        return sysDictDataMapper.selectAllSysDictdata(dicts);
    }
    @Override
    public List<SysDept> selectDeptList(SysDept dept) {
        return sysDeptMapper.selectDeptList(dept);
    }
    @Override
    public SysDept selectalldancha(Long deptId) {
        return sysDeptMapper.selectalldancha(deptId);
    }
    @Override
    public List<Long> selectDeptListByRoleId(Long roleId) {
        SysRole role = roleMapper.selectRoleById(roleId);
        return sysDeptMapper.selectDeptListByRoleId(roleId,role.isDeptCheckStrictly());
    }

    @Override
    public SysDept selectDeptById(Long deptId) {
        return null;
    }


    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
        List<SysDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public List<SysDept> buildDeptTree(List<SysDept> depts) {
        List<SysDept> sysDeptList=new ArrayList<SysDept>();
        List<Long> longs= new ArrayList<Long>();
        for (SysDept dept : depts) {
            longs.add(dept.getDeptId());
        }
        for (SysDept dept : depts) {
            if (!longs.contains(dept.getParentId())){
                recursionFn(depts, dept);
                sysDeptList.add(dept);
            }
        }
        if (sysDeptList.isEmpty())
        {
            sysDeptList = depts;
        }

        return sysDeptList;
    }

    private void recursionFn(List<SysDept> list, SysDept t)
    {

        List<SysDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    private List<SysDept> getChildList(List<SysDept> list, SysDept t)
    {
        List<SysDept> tlist = new ArrayList<SysDept>();
        Iterator<SysDept> it = list.iterator();
        while (it.hasNext())
        {
            SysDept n = (SysDept) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    private boolean hasChild(List<SysDept> list, SysDept t)
    {
        return getChildList(list, t).size() > 0;
    }
    @Override
    public int selectNormalChildrenDeptById(Long deptId) {
        return 0;
    }

    @Override
    public int hasChildByDeptId(Long deptId) {
        return 0;
    }

    @Override
    public int checkDeptExistUser(Long deptId) {
        return 0;
    }

    @Override
    public SysDept checkDeptNameUnique(String deptName, Long parentId) {
        return null;
    }

    @Override
    public int insertDept(SysDept sysDept) {
        return sysDeptMapper.insertDept(sysDept);
    }

    @Override
    public int updateDept(SysDept dept) {
        return sysDeptMapper.updateDept(dept);
    }

    @Override
    public List<SysDept> selectChildrenDeptById(Long deptId) {
        return sysDeptMapper.selectChildrenDeptById(deptId);
    }

    @Override
    public void updateDeptStatusNormal(Long[] deptIds) {

    }

    @Override
    public int updateDeptChildren(List<SysDept> depts) {
        return 0;
    }

    @Override
    public int deleteDeptById(Long deptId) {
        return 0;
    }

    @Override
    public int delete(Long deptId) {
        return sysDeptMapper.delete(deptId);
    }
}
